//package com.cq.hd.admin.shiro;
//
//import com.cq.hd.admin.po.TbAdminUserPo;
//import com.cq.hd.admin.service.TbAdminPermissionService;
//import com.cq.hd.admin.service.TbAdminUserService;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationException;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.HashSet;
//import java.util.Set;
//
//@Component
//public class MobileShiroRealm extends AuthorizingRealm {
//
//    @Resource
//    private TbAdminUserService adminUserService;
//    @Resource
//    private TbAdminPermissionService adminPermissionService;
//
//    @Override
//    public boolean supports(AuthenticationToken token) {
//        return token instanceof MobileToken;
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        MobileToken token;
//        // 如果是PhoneToken，则强转，获取phone；否则不处理。
//        if (authenticationToken instanceof MobileToken) {
//            token = (MobileToken) authenticationToken;
//        } else {
//            return null;
//        }
//
//        String mobile = (String) token.getPrincipal();
//        TbAdminUserPo user = adminUserService.getUserByMobile(mobile);
//        if (user == null) {
//            throw new UnknownAccountException("手机号错误");
//        }
//
//        if (user.getStatus() == null || user.getStatus() != 0) {
//            throw new UnknownAccountException("您已被禁用！");
//        }
//
//        return new SimpleAuthenticationInfo(user, mobile, this.getName());
//    }
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        if (principalCollection == null) {
//            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
//        }
//        TbAdminUserPo adminUserEntity = (TbAdminUserPo) getAvailablePrincipal(principalCollection);
//        Integer roleIds = adminUserEntity.getRoleId();
//        Set<String> roleSet = new HashSet<>();
//        roleSet.add(roleIds.toString());
//        Set<String> permissions = adminPermissionService.getSetByRoleIds(roleIds);
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.setRoles(roleSet);
//        simpleAuthorizationInfo.setStringPermissions(permissions);
//        return simpleAuthorizationInfo;
//    }
//
//}
